{% extends 'app01/backend.html' %}

{% block person_manage %}
    <a class="navbar-brand" href="#" xmlns="http://www.w3.org/1999/html">发布帖子</a>
{% endblock %}

{% block all_li %}
    <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab"
                                              data-toggle="tab">发布帖子</a></li>
    <li role="presentation"><a href="/app01/backend/">返回个人管理</a>
    </li>
{% endblock %}
{% block article %}
    <h3 class="text-center">发布帖子</h3>
    {#    利用form表单提交数据#}
    <form action="" method="post">
        {% csrf_token %}
        <p>标题</p>
        <div>
            <input type="text" name="title" class="form-control" id="title">
        </div>
        <p>内容</p>
        <div><textarea name="content" id="id_content01" rows="15" cols="100" class="form-control"></textarea></div>
        <p><b>分类</b></p>
        <div>

            {% for category in category_obj %}
                {% if forloop.first %}<!--为了给单选标签选上一个-->
                    {{ category.name }}
                    <input type="radio" name="category" value="{{ category.pk }}" class="radio_c" checked>&nbsp;&nbsp;
                {% else %}
                    {{ category.name }}<input type="radio" name="category" value="{{ category.pk }}" class="radio_c">
                {% endif %}



            {% endfor %}
            <button type="button" class="btn btn-success pull-right" id="add_article_id">新增帖子分类</button>

        </div>
        <hr>
        <p><b>标签</b></p>
        <div>
            {% for tag in tag_obj %}
                {{ tag.name }}<input type="checkbox" name="tag" value="{{ tag.pk }}" class="checkbox_c"> &nbsp;&nbsp;
            {% endfor %}

            <button type="button" class="btn btn-success pull-right" id="add_tag_id">新增帖子标签</button>
        </div>
        <hr>
        <button class="btn btn-block btn-primary" id="submit_id">提交</button>
        <hr>


    </form>
{% endblock %}



{% block js %}

    {% load static %}
    <script charset="utf-8" src="{% static 'kindeditor/kindeditor-all.js' %}"></script>
    <script>
        KindEditor.ready(function (K) {
            window.editor = K.create('#id_content01', {
                height: '500px',
                uploadJson: '/app01/upload/img/',//上传图片的后端的提交路径，由专门的视图函数来处理
                extraFileUploadParams: {//上传文件需要额外的参数,csrf的token
                    'csrfmiddlewaretoken': '{{ csrf_token }}'
                }

            });
        })
        //老师和学生的个人主页显示是不一样的
        if ('{{ request.user.username }}'.slice(0, 3) === '512') {
            $('#p_for_a').html('<a href="/app01/manage_student/" class="list-group-item">学生管理</a>')
            $('#role_for').html('角色：<br><a href="#">老师</a>')
        }

        if ('{{ request.user.username }}'.slice(0, 3) === '312') {

            $('#set_person_id').html('<p>班级：<input type="text" id="stu_class" class="form-control" value="{{ request.user.stu_class }}"></p>')
            $('#role_for').html('角色：<br><a href="#">学生</a>')
        }

          //学生才有班级
     if('{{ request.user.username }}'.slice(0,3)==='312'){
        $('#MyClass').html("<a href='/app01/myclass/'> 我的班级 </a>")
    }

    //新增标签和分类
    $('#add_article_id').click(function () {
       let category_name=prompt('输入新的文章分类')
        if(category_name != null){
            $.ajax({
            url:'',
            type:'post',
            data:{'name':category_name,'OP':'Category','str':'分类'},
            success:function (attr) {
                if (attr.code===100){
                    alert(attr.msg)
                    window.location.reload()
                }
                else {
                    alert(attr.msg)
                }

            }
        })
        }

    });

    $('#add_tag_id').click(function () {
       let category_name=prompt('输入新的文章标签')
        if(category_name != null){
            $.ajax({
            url:'',
            type:'post',
            data:{'name':category_name,'OP':'Tag','str':'标签'},
            success:function (attr) {
                if (attr.code===100){
                    alert(attr.msg)
                    window.location.reload()
                }
                else {
                    alert(attr.msg)
                }

            }
        })
        }

    })
 //文件上传的ajax请求
        $("#up_btn_id").click(function () {
            let formObj = new FormData()
            formObj.append('file', $('#up_file_id')[0].files[0])
            formObj.append('filename', $('#file_name_id').val())
            formObj.append('desc', $('#file_desc_id').val())
            let radio_chioce = $('input[name="share"]:checked').val();
            formObj.append('share', radio_chioce)

            $.ajax({
                url: "/app01/upload_file/",
                type: 'post',
                data: formObj,
                contentType: false,
                processData: false,
                success: function (array) {
                    if (array.code === 1000) {
                        alert(array.msg0)
                        parent.location.href = array.url
                    } else if (array.code === 2000) {
                        //用户输入不正确的提示
                        $('#p1').next().text(array.msg1)
                        $('#p2').next().text(array.msg2)
                        $('#p3').next().text(array.msg3)
                    } else {
                        alert(array.msg0)
                    }

                }
            })


        });

    </script>



{% endblock %}

